import moment from 'moment';
import { message } from 'antd'
import { observable, action, computed } from 'mobx';
import { updataIndicatrixState } from '../fetchHandler'

export default class IndicatrixModel {
  @observable state = 0;

  constructor(modelData, uiStore) {
    this.id = this.key = modelData.id;
    this.description = modelData.description;
    this.gmtCreate = moment(modelData.gmtCreate).format('YYYY-MM-DD HH:mm:ss');
    // this.indexNumber = modelData.number;               //指标编号
    this.status = modelData.status||0;                       //指标状态
    this.name = modelData.name;                        //指标名称
    this.meaningCode = modelData.meaningCode;   //含义代码
    this.type = modelData.type;                   //指标类别  basic普通指标
    this.calculationSpecification = modelData.calculationSpecification; //指标计算
    this.calculationSpecificationName = modelData.calculationSpecificationName; //指标计算显示名
    this.masterField = modelData.masterField;        //主属性

    this.uiStore = uiStore;
  }

  @action updataState(){
    this.uiStore.setLoading(true);

    updataIndicatrixState({
      indexId: this.id,
      status: this.status == 0?1:0
    })
    .then(res => {
      if (res.reasonCode == 200) {
        this.status = this.status == 0?1:0;
      } else {
        message.info(res.message || '请求失败');
      }
      this.uiStore.setLoading(false);
    })
    .catch(e => {
      message.error('请求失败');
      this.uiStore.setLoading(false);
    })


  }
}
